﻿<div ng-controller="Customer_AccountListC">
    <div class="bg-light lter b-b wrapper-md">
        <h1 class="m-n font-thin h3">户头管理</h1>
    </div>
    <div class="wrapper-md" style="text-align: right;">
        <div class="panel panel-default">
            <div class="panel-heading">
                <!--<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12" style="float: right">-->
                   <button class="btn btn-sm btn-default" ng-click="back()">
                    <span class="text">返回</span>
                   </button>
                    <button class="btn btn-sm btn-default" ng-click="SetNowEdit(null,true)" data-toggle="modal" data-target="#dlg">
                        <i class="fa fa-plus text"></i>
                        <span class="text">添加</span>
                    </button>

                <!--</div>-->
                <!--<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12 input-group">-->
                    <!--<input type="text" class="form-control" placeholder="按名称查找" ng-model="findname">-->
                    <!--<span class="input-group-btn">-->
                        <!--<button class="btn btn-default" type="button" ng-click="find()">查找</button>-->
                    <!--</span>-->
                <!--</div>-->
            </div>
            <div class="table-responsive text-center">
                <table id="example1" class="table table-striped b-t b-light">
                    <thead>
                    <tr>
                        <th style="text-align: center;">名称</th>
                        <th style="text-align: center;">银行</th>
                        <th style="text-align: center;">账号</th>
                        <th style="text-align: center;">备注</th>
                        <th style="text-align: center;">操作</th>

                    </tr>
                    </thead>
                    <tbody>
                    <tr ng-repeat="item in data.Accounts">
                        <td style="width: 15%">{{item.Name}}</td>
                        <td style="width: 15%">{{item.BankName}}</td>
                        <td style="width: 10%">{{item.BankNumber}}</td>
                        <td style="width: 10%">{{item.Remark}}</td>
                        <td style="width: 20%">

                            <button class="btn btn-sm btn-default" ng-click="SetNowEdit(item)" data-toggle="modal" data-target="#dlg">
                                <i class="fa fa-plus text"></i>
                                <span class="text">管理</span>
                            </button>
                        </td>

                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="modal fade" id="dlg" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="exampleModalLabel">账户信息</h4>
                </div>
                <form name="form" class="form-validation">
                    <div class="modal-body">
                        <div class="form-group">
                            <label for="recipient-name" class="control-label">名称</label>
                            <input required type="text" class="form-control" id="recipient-name" ng-model="NowData.Name">
                        </div>
                        <div class="form-group">
                            <label class="control-label">银行</label>
                            <input required type="text" class="form-control" ng-model="NowData.BankName">

                        </div>
                        <div class="form-group">
                            <label class="control-label">帐号(只能是数字)</label>
                            <input required type="text" class="form-control" ng-model="NowData.BankNumber" ng-pattern="/^[0-9]*$/">
                        </div>
                        <div class="form-group">
                            <label class="control-label">备注(非必填)</label>
                            <input type="text" class="form-control" ng-model="NowData.Remark">
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" ng-disabled="form.$invalid" data-dismiss="modal" ng-hide="NowData.isNew" ng-click="edit()">修改</button>
                        <button type="button" class="btn btn-default" ng-disabled="form.$invalid" data-dismiss="modal" ng-show="NowData.isNew" ng-click="add()">新建</button>
                        <button type="button" class="btn btn-primary" data-dismiss="modal">关闭</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script>
      angular.module('app') 
.controller("Customer_AccountListC", function ($scope, $state, $http, $stateParams) {
    console.log($stateParams.id);
    $http.get("/api/Customer/Detail",{params:{"id": $stateParams.id}})
       .then(function (res) {
           $scope.data = res.data;
           console.log($scope.data);
       })
       .then(function (error) {
           if (error) {
               alert("数据获取失败");
           }
       });
 $scope.SetNowEdit= function(data,isNew) {
     if (isNew) {
         $scope.NowData = {};
         $scope.NowData.isNew = true;
         $scope.NowData.CustomerId = $stateParams.id;
     } else {
         $scope.NowData = data;
     }
     
 };
          $scope.edit = function () {
        console.log($scope.NowData);
              $http.put("../api/Customer/EditAccount", $scope.NowData)
                      .success(function(res){
                          if(res.State<=0){
                              alert(res.Msg);
                          }else {
                              alert("修改成功");
                              $state.go('app.Customer_AccountList', { 'id': $stateParams.id });
                          }
                      });
    };
    $scope.add = function () {
        console.log($scope.NowData);
        $http.post("../api/Customer/AddAccount?customerId=" + $scope.NowData.CustomerId, $scope.NowData)
                .success(function(res){
                    if(res.State<=0){
                        alert(res.Msg);
                    }else {
                        alert("保存成功");
                        $state.go('app.Customer_AccountList', { 'id': $stateParams.id });
                    }
                });
    };
//    返回上一页
    $scope.back = function () {
        $state.go(window.history.back(-1));
    };
})
</script>
